/*
Copyright (c) 2011, Carsten Kolve.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the names of PointAttribViz
nor the names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

// Houdini
#include <OP/OP_OperatorTable.h>
#include <OP/OP_Operator.h>
#include <UT/UT_Math.h>
#include <UT/UT_Interrupt.h>
#include <GU/GU_Detail.h>
#include <CH/CH_LocalVariable.h>
#include <PRM/PRM_Include.h>
#include <GB/GB_ExtraMacros.h>

// PointAttribViz
#include "SOP_PointAttribViz.h"

#define debugme 1 

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// attribute name definitions


// primary position
static PRM_Name positionAAttribName("posaattrib", "Position A Attrib");
static PRM_Default positionAAttribDefault(0.0,"P");

// vector b
static PRM_Name vectorBModeName("vecbmode", "Vector B Mode");
// use default or attrib menu
static PRM_Name vectorBModeItems[] = {
	PRM_Name("vecbdef", "Default Vec B"),
	PRM_Name("vecbattrib",  "Vec B Attribute"),
	PRM_Name(0),
};
static PRM_ChoiceList vectorBModeMenu((PRM_ChoiceListType)
                                  (PRM_CHOICELIST_EXCLUSIVE | 
                                   PRM_CHOICELIST_REPLACE),
                                 vectorBModeItems);
static PRM_Name vectorBName("vecbdef", "Default Vector B");
static PRM_Name vectorBAttribName("vecbattrib", "Vector B Attrib");
static PRM_Default vectorBAttribDefault(0.0,"N");

// viz type
static PRM_Name vizTypeName("viztype", "Visualization Type");
static PRM_Name vizTypeItems[] = {
	PRM_Name("ptposa",    	"Point at Pos A"),
	PRM_Name("lnposavecb",  "Line from Pos A in Direction and with Length of Vec B"),
	PRM_Name("lnposauvecb", "Line from Pos A in Direction of Vec B"),
	PRM_Name("lnposaposb",  "Line from Pos A to Pos B"),
	PRM_Name(0),
};
static PRM_ChoiceList vizTypeMenu((PRM_ChoiceListType)
                                  (PRM_CHOICELIST_EXCLUSIVE | 
                                   PRM_CHOICELIST_REPLACE),
                                 vizTypeItems);

// scale
static PRM_Name scaleModeName("scalemode", "Scale Mode");
static PRM_Name scaleModeMenuItems[] = {
	PRM_Name("scaledef",     "Default Scale"),
	PRM_Name("sattrib",    "Scale Attrib Float / Vec X Comp"),
	PRM_Name("sattribvy",  "Scale Attrib Vec Y Comp"),
	PRM_Name("sattribvz",  "Scale Attrib Vec Z Comp"),
	PRM_Name("sattribvl",  "Scale Attrib Vec Length"),
	PRM_Name(0),
};
static PRM_ChoiceList scaleModeMenu((PRM_ChoiceListType)
                                  (PRM_CHOICELIST_EXCLUSIVE | 
                                   PRM_CHOICELIST_REPLACE),
                                 scaleModeMenuItems);
static PRM_Name scaleName("scaledef", "Default Scale");
static PRM_Name scaleAttribName("scaleattrib", "Scale Attrib");
static PRM_Name scaleAttribRangeName("scaleattribrange", "Scale Attrib Range");
static PRM_Name scaleDisplayRangeName("scaledisplayrange", "Scale Display Range");

// line width
static PRM_Name lineWidthName("linewidth", "Line Width");

// color
static PRM_Name colorModeName("colmode", "Color Mode");
static PRM_Name colorModeMenuItems[] = {
	PRM_Name("coldef", "Default Color"),
	PRM_Name("colattrib",   "Color Attrib"),
	PRM_Name("rattribfl",  "Ramp Attrib Float / Vec X Comp"),
	PRM_Name("rattribvy",  "Ramp Attrib Vec Y Comp"),
	PRM_Name("rattribvz",  "Ramp Attrib Vec Z Comp"),
	PRM_Name("rattribvl",  "Ramp Attrib Vec Length"),
	PRM_Name(0),
};
static PRM_ChoiceList colorModeMenu((PRM_ChoiceListType)
                                  (PRM_CHOICELIST_EXCLUSIVE | 
                                   PRM_CHOICELIST_REPLACE),
                                 colorModeMenuItems);


static PRM_Name colorName("coldef", "Default Color");
static PRM_Name colorAttribName("colattrib", "Color / Ramp Attrib");
static PRM_Name colorRampRangeName("colramprange", "Color Ramp Range");
static PRM_Name colorRampName("colramp", "Color Ramp");


// offset
static PRM_Name offsetName("offset", "Offset");

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Add parameters to SOP
PRM_Template SOP_PointAttribViz::myTemplateList[] = {
	// group
	PRM_Template( PRM_STRING, 1, &PRMgroupName, 0, &SOP_Node::pointGroupMenu),

	// viz type
	PRM_Template(PRM_ORD,    1, &vizTypeName, PRMzeroDefaults,&vizTypeMenu),
	
	// position attrib a
	PRM_Template( PRM_STRING, 1, &positionAAttribName, &positionAAttribDefault),

	// vector attrib b
	PRM_Template( PRM_ORD, 1, &vectorBModeName, PRMzeroDefaults ,&vectorBModeMenu),
	PRM_Template( PRM_XYZ, 3, &vectorBName , PRMposDefaults ),
	PRM_Template( PRM_STRING, 1, &vectorBAttribName, &vectorBAttribDefault),

	// scale
	PRM_Template( PRM_ORD, 1, &scaleModeName, PRMzeroDefaults ,&scaleModeMenu),
	PRM_Template( PRM_FLT, 1, &scaleName, PRMoneDefaults, 0, &PRMscaleRange ),
	PRM_Template( PRM_STRING, 1, &scaleAttribName),
	PRM_Template( PRM_FLT_MINMAX, 2, &scaleAttribRangeName),
	PRM_Template( PRM_FLT_MINMAX, 2, &scaleDisplayRangeName),

	// line width
	PRM_Template( PRM_FLT, 1, &lineWidthName, PRMtenDefaults, 0, &PRMscaleRange ),

	// offset
	PRM_Template( PRM_XYZ, 3, &offsetName , PRMposDefaults ),

	// color
	PRM_Template( PRM_ORD, 1, &colorModeName, PRMzeroDefaults ,&colorModeMenu),	
	PRM_Template( PRM_RGB, 3, &colorName , PRMpointFiveDefaults ),
	PRM_Template( PRM_STRING, 1, &colorAttribName),
	PRM_Template( PRM_FLT_MINMAX, 2, &colorRampRangeName),
	PRM_Template( PRM_MULTITYPE_RAMP_RGB, NULL, 1, &colorRampName),
   
	
};

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Empty variables
CH_LocalVariable SOP_PointAttribViz::myVariables[] = {
		{ 0, 0, 0 },
};

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Houdini's static creator method
OP_Node *SOP_PointAttribViz::myConstructor( OP_Network *net, const char *name, OP_Operator *op )
{
    return new SOP_PointAttribViz(net, name, op);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// constructor
SOP_PointAttribViz::SOP_PointAttribViz( OP_Network *net, const char *name, OP_Operator *op )
	:SOP_Node(net, name, op)
	,myGroup(0)
{}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// destructor
SOP_PointAttribViz::~SOP_PointAttribViz()
{}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Cook the SOP! This method does all the work
OP_ERROR SOP_PointAttribViz::cookMySop(OP_Context &context)
{
	#ifdef debugme
	std::cout << "SOP_PointAttribViz::cookMySop" << std::endl;
	#endif

	if (lockInputs(context) >= UT_ERROR_ABORT)
		return error();

	// duplicate the input
	duplicatePointSource(0, context);

	// get attributes
	// m_colorAttr = evalInt("enableGroupColor", 0, now);
	// evalString(m_colorAttrString, "groupColorAttribute", 0, now);

	// start our work (just add a detail attribute to the gdp and pass it on)
	UT_Interrupt *boss = UTgetInterrupt();
	boss->opStart("SOP_PointAttribViz::cookMySop...");


	bool m_dirty;
	useInputSource( 0, m_dirty, false );

	GU_DetailHandle inputHandle = inputGeoHandle( 0 );
	GU_DetailHandleAutoReadLock readHandle( inputHandle );
	const GU_Detail *inputGdp = readHandle.getGdp();
	// todo, do more stuff here - create display lists etc for standard geo etc

	if ( inputGdp )
	{
		if ( inputGdp->attribs().find( "SOP_PointAttribViz", GB_ATTRIB_MIXED ) )
		{
			std::cout << "found SOP_PointAttribViz detail" << std::endl;
		}
	}

	// pass this sop instance to the GR render hook via a detail attribute
	SOP_PointAttribVizPassStruct pass_ptr( this );
	gdp->addAttrib( "SOP_PointAttribViz",sizeof(SOP_PointAttribVizPassStruct), GB_ATTRIB_MIXED, &pass_ptr );

	// did the user interrupt us?
	// if (boss->opInterrupt())
	//	break;

	// end our work, tidy up & go home!
	boss->opEnd();
	unlockInputs();

	return error();
}


